<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="公司部门" prop="deptId">
        <treeselect style="width: 200px" v-model="queryParams.deptId" :options="deptOptions" placeholder="请选择公司部门" />
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!--<el-form-item label="订单号" prop="orderCode">-->
        <!--<el-input-->
          <!--v-model="queryParams.orderCode"-->
          <!--placeholder="请输入订单号"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="订单状态">-->
        <!--<el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable size="small" prop="status">-->
          <!--<el-option-->
            <!--v-for="dict in statusOptions"-->
            <!--:key="dict.dictValue"-->
            <!--:label="dict.dictLabel"-->
            <!--:value="dict.dictValue"-->
          <!--/>-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="订单状态" prop="orderStatus">-->
        <!--<el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable size="small">-->
          <!--<el-option label="请选择字典生成" value="" />-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="时间范围">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>




      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <!--<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
      <!--<el-form-item style="float: right;"><span style="font-size: 25px">平台总抽成：{{totalMoney}}</span></el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8">

    </el-row>

    <el-table   show-summary v-loading="loading" :data="orderList"  :summary-method="getSummaries" @selection-change="handleSelectionChange">

      <!--<el-table-column label="订单id" align="center" prop="orderId" />-->
      <el-table-column label="公司部门名称" align="center" prop="deptName" />
      <el-table-column label="订单号" align="center" prop="orderCode" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="单抽成" align="center" prop="money" />
      <el-table-column label="消费金额" align="center" prop="singleMoney" />
      <!--<el-table-column label="订单号" align="center" prop="typeDetailId" />-->
      <!--<el-table-column label="用户id" align="center" prop="mobileUserId" />-->
      <!--<el-table-column label="公司id" align="center" prop="deptId" />-->
      <!--<el-table-column label="工人id" align="center" prop="userId" />-->
      <!--<el-table-column label="订单状态 " align="center" prop="orderStatus":formatter="statusFormat" />-->
      <!--<el-table-column label="订单总金额" align="center" prop="totalMoney" />-->
      <!--<el-table-column label="实付金额" align="center" prop="money" />-->
      <!--<el-table-column label="优惠金额" align="center" prop="couponMoney" />-->
      <!--<el-table-column label="上门时间" align="center" prop="serviceTime" width="180">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.serviceTime) }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="支付方式 1-支付宝 2-微信(数据字典)" align="center" prop="payType" />-->
      <!--<el-table-column label="支付时间" align="center" prop="payTime" width="180">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.payTime) }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="项目名称" align="center" prop="serviceName" />-->
      <!--<el-table-column label="姓名" align="center" prop="name" />-->
      <!--<el-table-column label="联系方式" align="center" prop="phone" />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.serviceTime) }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->

      <!--<el-table-column label="指派公司" align="center" prop="deptName" />-->
      <!--<el-table-column label="服务人员" align="center" prop="workerName" />-->

      <!--<el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
        <!--<template slot-scope="scope">-->
          <!--<el-button-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-edit"-->
            <!--v-if = "!(scope.row.userId)&& !(scope.row.deptId)&&(scope.row.orderStatus == 1)"-->
            <!--@click="handleUpdate(scope.row)"-->
            <!--v-hasPermi="['system:returnMoney:edit']"-->

          <!--&gt;派单公司</el-button>-->
          <!--<el-button-->
            <!--v-if = "!(scope.row.userId)&&!(scope.row.deptId)&&(scope.row.orderStatus == 1)"-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-edit"-->
            <!--@click="handlePerson(scope.row)"-->
            <!--v-hasPermi="['system:returnMoney:edit']"-->
          <!--&gt;派单个人</el-button>-->
          <!--<el-button-->
            <!--v-if="!(scope.row.userId)&&(scope.row.deptId)&&(scope.row.orderStatus == 1)"-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--icon="el-icon-edit"-->
            <!--@click="handle(scope.row)"-->
            <!--v-hasPermi="['system:returnMoney:edit']"-->
          <!--&gt;派单</el-button>-->
          <!--&lt;!&ndash;<el-button&ndash;&gt;-->
            <!--&lt;!&ndash;size="mini"&ndash;&gt;-->
            <!--&lt;!&ndash;type="text"&ndash;&gt;-->
            <!--&lt;!&ndash;icon="el-icon-delete"&ndash;&gt;-->
            <!--&lt;!&ndash;@click="handleDelete(scope.row)"&ndash;&gt;-->
            <!--&lt;!&ndash;v-hasPermi="['system:order:remove']"&ndash;&gt;-->
          <!--&lt;!&ndash;&gt;删除</el-button>&ndash;&gt;-->
        <!--</template>-->
      <!--</el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户服务订单对话框 -->
    <!--<el-dialog :title="title" :visible.sync="open" width="500px">-->
      <!--<el-form ref="form" :model="form" :rules="rules" label-width="80px" v-show="this.open1">-->
        <!--<el-form-item label="派单公司" prop="deptId">-->
          <!--<treeselect v-model="form.deptId" :options="deptOptions" placeholder="请选择公司部门" />-->
        <!--</el-form-item>-->
      <!--</el-form>-->
      <!--<el-form ref="form" :model="form" :rules="rules" label-width="80px" v-show="this.open2">-->
        <!--<el-form-item label="派单个人" >-->
        <!--<el-select v-model="person"  filterable placeholder="请选择工人"  value-key="userId">-->
          <!--<el-option-->
            <!--v-for="item in deptOptions"-->
            <!--:key="item.userId"-->
            <!--:label="item.userName"-->
            <!--:value="item">-->
          <!--</el-option>-->
        <!--</el-select>-->
        <!--</el-form-item>-->
      <!--</el-form>-->
      <!--<el-form ref="form" :model="form" :rules="rules" label-width="80px" v-show="this.open3">-->
        <!--<el-form-item label="派单" >-->
          <!--<el-select v-model="person"  filterable placeholder="请选择"  value-key="userId" >-->
            <!--<el-option-->
              <!--v-for="item in deptOptions"-->
              <!--:key="item.userId"-->
              <!--:label="item.userName"-->
              <!--:value="item">-->
            <!--</el-option>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <!--&lt;!&ndash;<el-form-item label="派单公司" prop="deptId">&ndash;&gt;-->
        <!--&lt;!&ndash;<treeselect v-model="form.deptId" :options="deptOptions" placeholder="请选择公司部门" />&ndash;&gt;-->
        <!--&lt;!&ndash;</el-form-item>&ndash;&gt;-->
      <!--</el-form>-->
      <!--<div slot="footer" class="dialog-footer">-->
        <!--<el-button type="primary" @click="submitForm">确 定</el-button>-->
        <!--<el-button @click="cancel">取 消</el-button>-->
      <!--</div>-->
    <!--</el-dialog>-->
  </div>
</template>

<script>
import { listreturn,treeselect,personlist,plist, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/system/returnMoney";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect },
  name: "Order",
  data() {
    return {
      // 遮罩层
      defaultProps: {
        children: "children",
        label: "label"
      },

      totalMoney:undefined,
      loading: true,
      // 选中数组
      ids: [],
      deptOptions: undefined,
      statusOptions:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      person:[],
      // 用户服务订单表格数据
      orderList: [],
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1:false,
      open2:false,
      open3:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: undefined,
        typeDetailId: undefined,
        mobileUserId: undefined,
        deptId: undefined,
        userId: undefined,
        orderStatus: undefined,
        payType: undefined,
        payTime: undefined,
        serviceName: undefined,
        name: undefined,
        phone: undefined,
        serviceTime: undefined,
        serviceDetail: undefined,
        addressId: undefined,
        cityId: undefined,
        orderType: undefined,
        money: undefined,
        totalMoney: undefined,
        tailMoney: undefined,
        skuId: undefined,
        data: undefined,
        couponId: undefined,
        couponMoney: undefined,
        // total: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getTreeselect();
    this.getList();
    this.getDicts("sys_order_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += ' ';
        } else {
          sums[index] = '';
        }
        sums[3]='';
        sums[5]='';
      });

      return sums;
    },
    /** 查询用户服务订单列表 */
    getList() {
      this.loading = true;
      listreturn(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.orderStatus);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.person = undefined,
      this.form = {
        orderId: undefined,
        orderCode: undefined,
        typeDetailId: undefined,
        mobileUserId: undefined,
        deptId: undefined,
        userId: undefined,
        orderStatus: "0",
        payType: undefined,
        payTime: undefined,
        serviceName: undefined,
        name: undefined,
        phone: undefined,
        serviceTime: undefined,
        serviceDetail: undefined,
        addressId: undefined,
        cityId: undefined,
        orderType: undefined,
        money: undefined,
        totalMoney: undefined,
        tailMoney: undefined,
        createTime: undefined,
        remark: undefined,
        skuId: undefined,
        data: undefined,
        couponId: undefined,
        couponMoney: undefined,
        total: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户服务订单";
    },

    /** 派单服务 */
    // handleUpdate(row) {
    //   treeselect().then(response => {
    //     this.deptOptions = response.data;
    //   });
    //   this.reset();
    //   this.open1 = true;
    //   this.open2 = false;
    //   this.open3 = false;
    //   console.log(row.userId)
    //   // const orderId = row.orderId || this
    //   this.form.orderId = row.orderId
    //   this.form.userId = row.userId
    //     this.open = true;
    //     this.title = "派单服务";
    //
    // },
    // handlePerson(row){
    //
    //   this.open1 = false ;
    //   this.open2 = true;
    //   this.open3 = false;
    //   personlist().then(response => {
    //     this.deptOptions = response.data;
    //
    //   });
    //   this.reset();
    //   this.form.orderStatus = 2;
    //   this.form.orderId = row.orderId
    //   this.open = true;
    //   this.title = "派单服务";
    // },
    // handle(row){
    //   this.open1 = false ;
    //   this.open2 = false;
    //   this.open3 = true;
    //   plist().then(response => {
    //     this.deptOptions = response.data;
    //     console.log(this.deptOptions)
    //   });
    //   this.reset();
    //   this.form.orderStatus = 1;
    //   this.form.orderId = row.orderId
    //   this.open = true;
    //   this.title = "派单服务";
    // },
    /** 提交按钮 */
    submitForm: function() {
      console.log(this.person)
      if(this.person) {
        this.form.deptId =this.person.deptId
        this.form.userId =this.person.userId
      }

      this.$refs["form"].validate(valid => {
        if (valid) {
          updateOrder(this.form).then(response => {

              if (response.code === 200) {
                this.msgSuccess("派单成功");
                this.open = false;
                this.getList();
                this.reset();
              } else {
                this.msgError(response.msg);

              }
            });

        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$confirm('是否确认删除用户服务订单编号为"' + orderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(orderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户服务订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
